package com.jdcjk.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


/**
 * TimeUtils
 * 获取本地时间的工具类
 *
 * @author <a href="http://www.trinea.cn" target="_blank">Trinea</a> 123-8-24
 */
public class TimeUtils {
    private static final String TAG = TimeUtils.class.getSimpleName();
    public static final SimpleDateFormat DEFAULT_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static final SimpleDateFormat DATE_FORMAT_DATE = new SimpleDateFormat("yyyy-MM-dd");
    public static final SimpleDateFormat MINUTE_FORMAT_DATE = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    public static final SimpleDateFormat MOVIE_DATE_FORMAT_DATE = new SimpleDateFormat("hh:mm:ss");

    private TimeUtils() {
        throw new AssertionError();
    }

    /**
     * long time to string
     *
     * @param timeInMillis
     * @param dateFormat
     * @return
     */
    public static String getTime(long timeInMillis, SimpleDateFormat dateFormat) {
        return dateFormat.format(new Date(timeInMillis));
    }

    /**
     * long time to string, format is {@link #DEFAULT_DATE_FORMAT}
     *
     * @param timeInMillis
     * @return
     */
    public static String getTime(long timeInMillis) {
        return getTime(timeInMillis, DEFAULT_DATE_FORMAT);
    }

    /**
     * Data时间转换成（yyyy-MM-dd HH:mm:ss）格式
     *
     * @param date 时间
     * @return
     */
    public static String getTime(Date date) {
        return DEFAULT_DATE_FORMAT.format(date);
    }

    /**
     * Data时间转换成指定格式
     *
     * @param date       时间
     * @param dateFormat 时间格式
     * @return
     */
    public static String getTime(Date date, SimpleDateFormat dateFormat) {
        return dateFormat.format(date);
    }

    /**
     * get current time in milliseconds
     *
     * @return
     */
    public static long getCurrentTimeInLong() {
        return System.currentTimeMillis();
    }

    /**
     * get current time in milliseconds, format is {@link #DEFAULT_DATE_FORMAT}
     *
     * @return
     */
    public static String getCurrentTimeInString() {
        return getTime(getCurrentTimeInLong());
    }

    /**
     * get current time in milliseconds
     *
     * @return
     */
    public static String getCurrentTimeInString(SimpleDateFormat dateFormat) {
        return getTime(getCurrentTimeInLong(), dateFormat);
    }

    /**
     * 对时间进行判断，改为 刚刚 一天前 10秒前的String
     */
    public static String simplifyTime(String time) {
        String timeString = "";
        int timeGap;
        Calendar lateC = Calendar.getInstance();
        Calendar nowC = Calendar.getInstance();
        try {
            lateC.setTime(DEFAULT_DATE_FORMAT.parse(time));
            long late = lateC.getTimeInMillis();
            String nowdata = DEFAULT_DATE_FORMAT.format(new Date());
            lateC.setTime(DEFAULT_DATE_FORMAT.parse(nowdata));
            long now = lateC.getTimeInMillis();
            long gap = now - late;
            gap = gap / 1000;
            if (gap / 60 < 1) {
                timeString = "刚刚";
            } else if (gap / 60 >= 1 && gap / 3600 < 1) {
                timeGap = (int) gap / 60;
                timeString = timeGap + "分钟前";
            } else if (gap / 3600 >= 1 && gap / 86400 < 1) {
                timeGap = (int) gap / 3600;
                timeString = timeGap + "小时前";
            } else if (gap / 86400 >= 1) {
                return time;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return timeString;
    }

    /**
     * 计算传入的时间与本地时间的差值
     *
     * @param time 参数时间
     */
    public static long getTheTimeDifference(String time) {
        long gap = 0;
        Calendar lateC = Calendar.getInstance();
        Calendar nowC = Calendar.getInstance();
        try {
            lateC.setTime(DEFAULT_DATE_FORMAT.parse(time));
            long late = lateC.getTimeInMillis();
            String nowdata = DEFAULT_DATE_FORMAT.format(new Date());
            lateC.setTime(DEFAULT_DATE_FORMAT.parse(nowdata));
            long now = lateC.getTimeInMillis();
            gap = now - late;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return gap;
    }

    /**
     * 计算传入的两个时间之间的差值（返回分钟）
     * 默认"yyyy-MM-dd HH:mm:ss"格式
     *
     * @param time1 时间1
     * @param time2 时间2
     * @return
     */
    public static int calculatingTimeDifferenceValue(String time1, String time2) {
        return calculatingTimeDifferenceValue(time1, time2, DEFAULT_DATE_FORMAT);
    }

    /**
     * 计算传入的两个时间之间的差值（返回分钟）
     *
     * @param time1 时间1
     * @param time2 时间2
     * @return
     */
    public static int calculatingTimeDifferenceValue(String time1, String time2, SimpleDateFormat simpleDateFormat) {
        long gap = 0;
        int timeGap = 0;
        Calendar lateC = Calendar.getInstance();
        Calendar nowC = Calendar.getInstance();
        try {
            nowC.setTime(simpleDateFormat.parse(time1));
            long now = nowC.getTimeInMillis();
            lateC.setTime(simpleDateFormat.parse(time2));
            long late = lateC.getTimeInMillis();
            gap = Math.abs(now - late);
            gap = gap / 1000;
            if (gap / 60 >= 1) {
                timeGap = (int) gap / 60;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return timeGap;
    }

    /**
     * 时间转换
     *
     * @param time 单位/s
     * @return
     */
    public static String timeTransformation(int time) {
        String timeString = "";
        if (time < 0 || time == 0) {
        } else if (time < 60) {
            timeString = String.valueOf(time) + "秒";
        } else if (time < 3600) {
            float a = (float) time / 60;
            //保留两位小数
            float b = (float) (Math.round(a * 100)) / 100;
            timeString = String.valueOf(b) + "分钟";
        } else if (time < 3600 * 24) {
            int a = time / 3600;
            time = time - a * 3600;
            int b = time / 60;
            timeString = String.valueOf(a) + "小时" + String.valueOf(b) + "分钟";
        } else {
            int a = time / (3600 * 24);
            time = (time - a * 3600 * 24);
            int b = time / 3600;
            timeString = a + "天" + b + "小时";
        }
        return timeString;
    }
}
